package com.soriya.nestlive.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soriya.nestlive.entity.LiveEntity;
import com.soriya.nestlive.entity.PageResult;
import com.soriya.nestlive.entity.vo.LiveDetailVo;
import com.soriya.nestlive.service.LiveService;
import com.soriya.nestlive.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/live")
public class LiveApi {

    private final LiveService liveService;

    @Autowired
    public LiveApi(LiveService liveService) {
        this.liveService = liveService;
    }

    @GetMapping("/channelId/{channelId}")
    public Result<?> getByChannelId(@PathVariable Long channelId) {
        LiveDetailVo liveDetail =  liveService.getByChannelId(channelId);
        return Result.success(liveDetail);
    }

    @GetMapping("/categoryId/{categoryId}/{current}/{size}")
    public Result<?> getByCategoryId(@PathVariable Long categoryId, @PathVariable Integer current, @PathVariable Integer size) {
        PageResult<LiveDetailVo> list = liveService.getByCategoryId(categoryId, current, size);
        return Result.success(list);
    }

    @GetMapping("/{current}/{size}")
    public Result<?> list(@PathVariable Integer current, @PathVariable Integer size) {
        Page<LiveDetailVo> page = new Page<>(current, size);
        PageResult<LiveDetailVo> pageResult = liveService.pageList(page);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result<?> save(@RequestBody LiveEntity liveEntity) {
        liveEntity.setStartTime(new Date());
        boolean save = liveService.save(liveEntity);
        return Result.update(save);
    }

    @GetMapping("/stopStream")
    public Result<?> stopStream(Long channelId) {
        boolean save = liveService.stopStream(channelId);
        return Result.success(save);
    }

    @GetMapping("/history/{channelId}")
    public Result<?> getHistory(@PathVariable Long channelId) {
        List<LiveEntity> list = liveService.getHistory(channelId);
        return Result.success(list);
    }

}
